package com.kexilo.core.common.core.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.kexilo.core.shared.dto.TreeNodeDTO;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构实体类
 * 
 * @author Kexilo
 */
public class TreeSelect implements Serializable {
    
    private static final long serialVersionUID = 1L;

    /** 节点ID */
    private Long id;

    /** 节点名称 */
    private String label;

    /** 子节点 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelect> children;

    public TreeSelect() {

    }

    /**
     * 从通用树形节点DTO构造TreeSelect
     * 
     * @param treeNode 树形节点DTO
     */
    public TreeSelect(TreeNodeDTO treeNode) {
        this.id = treeNode.getId();
        this.label = treeNode.getLabel();
        if (treeNode.getChildren() != null) {
            this.children = treeNode.getChildren().stream()
                    .map(TreeSelect::new)
                    .collect(Collectors.toList());
        }
    }
    
    /**
     * 从树形节点列表构造TreeSelect列表
     * 
     * @param treeNodes 树形节点列表
     * @return TreeSelect列表
     */
    public static List<TreeSelect> fromTreeNodes(List<TreeNodeDTO> treeNodes) {
        if (treeNodes == null) {
            return null;
        }
        return treeNodes.stream()
                .map(TreeSelect::new)
                .collect(Collectors.toList());
    }
    
    // Getter and Setter methods
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getLabel() {
        return label;
    }
    
    public void setLabel(String label) {
        this.label = label;
    }
    
    public List<TreeSelect> getChildren() {
        return children;
    }
    
    public void setChildren(List<TreeSelect> children) {
        this.children = children;
    }
}
